From f888ec564e06961b8f2657a34f511c538f8b46ee Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sat, 18 Nov 2023 15:16:01 -0700 Subject: [PATCH] convert tpg to dynamic Format class. (#1229) --- CMakeLists.txt | 1 + tpg.cc | 75 ++++++++++++++++---------------------------- tpg.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ vecs.cc | 6 ++-- 4 files changed, 114 insertions(+), 52 deletions(-) create mode 100644 tpg.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 17b57b335..6be14d150 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -238,6 +238,7 @@ set(HEADERS skytraq.h subrip.h text.h + tpg.h tpo.h unicsv.h units.h diff --git a/tpg.cc b/tpg.cc index 615a08de2..5b21552e3 100644 --- a/tpg.cc +++ b/tpg.cc @@ -22,16 +22,17 @@ */ +#include "tpg.h" + #include // for isalnum #include // for memcmp #include // for QChar #include // for QString -#include // for QVector #include "defs.h" -#include "gbfile.h" // for gbfwrite, gbfgetint16, gbfputint16, gbfclose -#include "jeeps/gpsmath.h" // for GPS_Lookup_Datum_Index, GPS_Math_Known_Da... +#include "gbfile.h" // for gbfwrite, gbfgetint16, gbfputint16, gbfclose, gbfgetdbl, gbfgetpstr, gbfopen_le, gbfputdbl, gbfgetint32, gbfputc, gbfputpstr, gbfread +#include "jeeps/gpsmath.h" // for GPS_Lookup_Datum_Index, GPS_Math_Known_Datum_To_WGS84_M, GPS_Math_WGS84_To_Known_Datum_M #include "mkshort.h" // for MakeShort @@ -40,21 +41,8 @@ #define MAXTPGSTRINGSIZE 256 #define MAXTPGOUTPUTPINS 65535 -static gbfile* tpg_file_in; -static gbfile* tpg_file_out; -static MakeShort* mkshort_handle; -static char* tpg_datum_opt; -static int tpg_datum_idx; - -static unsigned int waypt_out_count; - -static -QVector tpg_args = { - {"datum", &tpg_datum_opt, "Datum (default=NAD27)", "N. America 1927 mean", ARGTYPE_STRING, ARG_NOMINMAX , nullptr}, -}; - -static int -valid_tpg_header(char* header, int len) +int +TpgFormat::valid_tpg_header(char* header, int len) { unsigned char header_bytes[] = { 0xFF, 0xFF, 0x01, 0x00, 0x0D, 0x00, 0x43, 0x54, 0x6F, 0x70, @@ -68,8 +56,8 @@ valid_tpg_header(char* header, int len) return memcmp(header_bytes, header, len); } -static void -tpg_common_init() +void +TpgFormat::tpg_common_init() { tpg_datum_idx = GPS_Lookup_Datum_Index(tpg_datum_opt); if (tpg_datum_idx < 0) { @@ -77,21 +65,21 @@ tpg_common_init() } } -static void -tpg_rd_init(const QString& fname) +void +TpgFormat::rd_init(const QString& fname) { tpg_common_init(); tpg_file_in = gbfopen_le(fname, "rb", MYNAME); } -static void -tpg_rd_deinit() +void +TpgFormat::rd_deinit() { gbfclose(tpg_file_in); } -static void -tpg_wr_init(const QString& fname) +void +TpgFormat::wr_init(const QString& fname) { tpg_common_init(); tpg_file_out = gbfopen_le(fname, "wb", MYNAME); @@ -99,15 +87,15 @@ tpg_wr_init(const QString& fname) waypt_out_count = 0; } -static void -tpg_wr_deinit() +void +TpgFormat::wr_deinit() { delete mkshort_handle; gbfclose(tpg_file_out); } -static void -tpg_read() +void +TpgFormat::read() { char buff[MAXTPGSTRINGSIZE + 1]; double amt; @@ -169,8 +157,8 @@ tpg_read() } } -static void -tpg_waypt_pr(const Waypoint* wpt) +void +TpgFormat::tpg_waypt_pr(const Waypoint* wpt) { double lon, lat; double amt; @@ -280,8 +268,8 @@ tpg_waypt_pr(const Waypoint* wpt) } } -static void -tpg_write() +void +TpgFormat::write() { unsigned char header_bytes[] = { 0xFF, 0xFF, 0x01, 0x00, 0x0D, 0x00, 0x43, 0x54, 0x6F, 0x70, @@ -307,19 +295,8 @@ tpg_write() /* write the rest of the header */ gbfwrite(header_bytes, 1, 19, tpg_file_out); - waypt_disp_all(tpg_waypt_pr); + auto tpg_waypt_pr_lambda = [this](const Waypoint* waypointp)->void { + tpg_waypt_pr(waypointp); + }; + waypt_disp_all(tpg_waypt_pr_lambda); } - -ff_vecs_t tpg_vecs = { - ff_type_file, - FF_CAP_RW_WPT, - tpg_rd_init, - tpg_wr_init, - tpg_rd_deinit, - tpg_wr_deinit, - tpg_read, - tpg_write, - nullptr, - &tpg_args, - NULL_POS_OPS -}; diff --git a/tpg.h b/tpg.h new file mode 100644 index 000000000..6f4b098d3 --- /dev/null +++ b/tpg.h @@ -0,0 +1,84 @@ +/* + National Geographic Topo! TPG file support (Waypoints/Routes) + Contributed to gpsbabel by Alex Mottram + + For Topo! version 2.x. Routes are currently not implemented. + + Copyright (C) 2002 Alex Mottram, geo_alexm at cox-internet.com + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + */ +#ifndef TPG_H_INCLUDED_ +#define TPG_H_INCLUDED_ + +#include // for QString +#include // for QVector + +#include "defs.h" +#include "format.h" // for Format +#include "gbfile.h" // for gbfile +#include "mkshort.h" // for MakeShort + + +class TpgFormat : public Format +{ +public: + using Format::Format; + + QVector* get_args() override + { + return &tpg_args; + } + + ff_type get_type() const override + { + return ff_type_file; + } + + QVector get_cap() const override + { + return FF_CAP_RW_WPT; + } + + void rd_init(const QString& fname) override; + void read() override; + void rd_deinit() override; + void wr_init(const QString& fname) override; + void write() override; + void wr_deinit() override; + +private: + /* Member Functions */ + + static int valid_tpg_header(char* header, int len); + void tpg_common_init(); + void tpg_waypt_pr(const Waypoint* wpt); + + /* Data Members */ + + gbfile* tpg_file_in{}; + gbfile* tpg_file_out{}; + MakeShort* mkshort_handle{}; + char* tpg_datum_opt{}; + int tpg_datum_idx{}; + + unsigned int waypt_out_count{}; + + QVector tpg_args = { + {"datum", &tpg_datum_opt, "Datum (default=NAD27)", "N. America 1927 mean", ARGTYPE_STRING, ARG_NOMINMAX, nullptr}, + }; +}; +#endif // TPG_H_INCLUDED_ diff --git a/vecs.cc b/vecs.cc index 06bee8ecc..4807ad949 100644 --- a/vecs.cc +++ b/vecs.cc @@ -70,6 +70,7 @@ #include "src/core/logging.h" // for Warning, FatalMsg #include "subrip.h" // for SubripFormat #include "text.h" // for TextFormat +#include "tpg.h" // for TpgFormat #include "tpo.h" // for Tpo2Format, Tpo3Format #include "unicsv.h" // for UnicsvFormat #include "v900.h" // for V900Format @@ -81,7 +82,6 @@ extern ff_vecs_t geo_vecs; extern ff_vecs_t ozi_vecs; #if MAXIMAL_ENABLED -extern ff_vecs_t tpg_vecs; extern ff_vecs_t gpl_vecs; extern ff_vecs_t mtk_vecs; extern ff_vecs_t mtk_fvecs; @@ -121,7 +121,6 @@ struct Vecs::Impl { KmlFormat kml_fmt; #if MAXIMAL_ENABLED LowranceusrFormat lowranceusr_fmt; - LegacyFormat tpg_fmt {tpg_vecs}; Tpo2Format tpo2_fmt; Tpo3Format tpo3_fmt; #if SHAPELIB_ENABLED @@ -233,11 +232,12 @@ struct Vecs::Impl { nullptr, }, { - &tpg_fmt, + nullptr, "tpg", "National Geographic Topo .tpg (waypoints)", "tpg", nullptr, + &fmtfactory }, { &tpo2_fmt, -- 2.30.2